﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using LitJson;
using BusinessLogic.ClassFactory;
using HtmlAgilityPack;

namespace RichECMS.news
{
    public partial class Load_style : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strReq = Request["uuid"] ?? "";
            strReq = strReq.Replace("style_id_", "");
            if (!ValidateCommon.ParamAuth.IsNumber(strReq))
            {
                strReq = "-1";
            }
            Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
            Response.Write(BuilderResponse(strReq));
            Response.End();
            return;
        }

        protected string BuilderResponse(string intArtSecID)
        {
            Hashtable objHashTable = new Hashtable();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" intSecArtID=" + intArtSecID + " ");
            using (ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory())
            {
                int secid = 0;
                BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.CreatorBySql(strWhere.ToString());
                if (objArtSecRelation != null)
                {
                    secid = objArtSecRelation.DataItem.intSectionID;

                    ArticleFactory objArticleFactory = new ArticleFactory();
                    BusinessLogic.BusinessRules.Article objArticle = objArticleFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                    if (objArticle != null)
                    {
                        ArrayList imgnini = new ArrayList();
                        ArrayList imgbig = new ArrayList();

                        imgnini.Add("/news/web/pcfiles/" + objArticle.DataItem.chvMainMiniPic);
                        imgbig.Add("/news/web/pcfiles/" + objArticle.DataItem.chvMainPic);

                        if (!objArticle.DataItem.IschvSummaryNull())
                        {
                            HtmlDocument htm = new HtmlDocument();
                            htm.LoadHtml(objArticle.DataItem.chvSummary);

                            //筛选img标签
                            HtmlNodeCollection imgparent = htm.DocumentNode.SelectNodes("//img");
                            if (imgparent != null)
                            {
                                if (imgparent.Count > 0)
                                {
                                    for (int i = 0; i < imgparent.Count; i++)
                                    {
                                        if (imgparent[i].Attributes["src"] != null)
                                        {
                                            if (imgparent[i].Attributes["src"].Value.Trim() != "")
                                            {
                                                string strSrc = imgparent[i].Attributes["src"].Value.Replace("../","/news/web/");
                                                imgbig.Add(strSrc);
                                                imgnini.Add(strSrc.Replace(".","mini."));
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        //vedio
                        Hashtable objvedio = new Hashtable();
                        objvedio["web_style_video"] = "";
                        objvedio["web_look_video"] = "";
                        objHashTable["videos"] = objvedio;

                        //info
                        objHashTable["bullets"] = objArticle.DataItem.chvDescription;
                        objHashTable["style_id"] = "style_id_" + intArtSecID;
                        objHashTable["style_code"] = "style_code_" + intArtSecID;
                        objHashTable["personalizable"] = false;
                        objHashTable["gender_id"] = intArtSecID;
                        objHashTable["main_text"] = objArticle.DataItem.chvTitle;

                        //image
                        Hashtable objImageTital = new Hashtable();
                        objImageTital["no_image"] = "/news/web/resource/images/blanks/photo_unavailable_web_full.jpg";
                        objImageTital["web_6up"] = "/news/web/pcfiles/" + objArticle.DataItem.chvMainMiniPic;
                        objImageTital["web_6upquickview"] = "/news/web/pcfiles/" + objArticle.DataItem.chvMainMiniPic;
                        objImageTital["web_doublehero"] = "/news/web/pcfiles/" + objArticle.DataItem.chvMainMiniPic;

                        string[] strWebFull = (String[])imgnini.ToArray(typeof(string));
                        objImageTital["web_full"] = strWebFull;

                        objImageTital["web_look_minithumb"] = "";
                        objImageTital["web_look_variation"] = "";
                        objImageTital["web_look_video_minithumb"] = "";
                        objImageTital["web_look_zoomout"] = "";
                        objImageTital["web_minithumb"] = "/news/web/resource/images/ecommerce/styles_new/201301/web_minithumb/323672_A7M0G_7009_007_web_minithumb.jpg";

                        string[] strweb_variation = (String[])imgnini.ToArray(typeof(string));

                        objImageTital["web_variation"] = strweb_variation;

                        string[] strweb_zoomin = (String[])imgbig.ToArray(typeof(string));

                        objImageTital["web_zoomin"] = strweb_zoomin;

                        objImageTital["web_zoomout"] = "12";
                        objHashTable["images"] = objImageTital;
                    }
                }
            }

            return JsonMapper.ToJson(objHashTable);
        }
    }
}